
<%@page contentType="text/html" language="java" import="com.compuvest.web.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<%@ taglib uri="/WEB-INF/tld/bottom.tld" prefix="bottom" %>
<%@ taglib uri="/WEB-INF/tld/head.tld" prefix="head" %>

<jsp:useBean id="sb" class="MySQL" scope="application"/>
<jsp:useBean id="advertArray" class="AdvertArray" scope="application"/>
<jsp:useBean id="basket" class="Basket" scope="session"/>
<%!
    public String selectWarranty(int pos, double warrantyPrice1, double warrantyPrice2, double warrantyPrice3, String desc) {
	double price = 0d;
	switch(pos) {
	    case 1  : price = warrantyPrice1; break;
	    case 2  : price = warrantyPrice2; break;
	    case 3  : price = warrantyPrice3; break;
	    default : price = 0d;
	}
	return price == 0d ? "" : String.format("<option value='%s|%.2f'/>+ %s $%,.2f", desc, price, desc, price);
//	return String.format("<option value='%s|%.2f'/>+ %s $%,.2f", desc, price, desc, price);
//	return price == 0d ? "<option value='0|0.0'/>No Thanks" : String.format("<option value='%s|%.1f'/>%s $%,.2f", desc, price, desc, price);
    }

    Items basketItems = new Items(); // for getResellerPriceLine()
%>
<%
    String checkoutURL = application.getInitParameter("https") + "CustomerForm.jsp";

    Customer cust = basket.getCustomer();
    if(cust != null) {
        checkoutURL = application.getInitParameter("https") + "Payment.jsp";
    }

    java.text.DecimalFormat priceForm = new java.text.DecimalFormat("$#,##0.00");

    String countryCode = advertArray.getString(request.getParameter("CountryCode"), "US");
    String zipCode = advertArray.getString(request.getParameter("ZipCode"), "");

    basket.setShippingCost(0d);

    // update quantity
    String[] quantity = request.getParameterValues("quantity");
    String[] quantityID = request.getParameterValues("qtyID");
    String[] warranties = request.getParameterValues("warranty");
    if(quantity != null) {
	for(int i = 0; i < quantity.length; i++) {
	    basket.setQuantity(advertArray.getInt(quantityID[i], 0), advertArray.getInt(quantity[i], 0), warranties[i]);
	}
    }

    // delete items
    String[] deleteIDs = request.getParameterValues("delete");
    if(deleteIDs != null) {
	for(String itemID : deleteIDs) {

	    basket.setQuantity(advertArray.getInt(itemID, 0), 0, null);
	}
    }

    // add item
    String iidd = request.getParameter("iidd");
    if(iidd != null) {

	int itemID = advertArray.getInt(iidd, 0);
	if(itemID != 0) {
	    basket.addItem(new ItemsInBasket(sb.getItemForBasket(itemID), true));
	}
    }
%>
<html>
<%@include file="/incl/head.html" %>

<body>

<table style='height:100%;'>

<tr valign=top height=84>
 <td colspan=3>

<head:show />

 </td>
</tr>

<tr valign=top align=center>
 <td style="width:160px;">&nbsp;</td>

 <td>
<%
    if(basket.size() != 0) {
%>

 <br/>
  <div class='messb'>To order from our site you need to have a reseller account.
  If you don't have one yet, it's quick and easy, click 
  <a href='<%= application.getInitParameter("https") + "Reseller.jsp" %>'><font size='+1'>here</font></a>
  to set up an account now.
  Reseller Certificate required.</div>
<br/>
 <form name='basket' action='<%= application.getInitParameter("http") + "Basket.jsp" %>' method='POST'>
 <div class='htlt'>Shopping Cart</div>
 <table class='part' width='80%' border=1 cellpadding=3 style='border-collapse:collapse;background-color:white;border:1px solid #d4d4d4;'>
  <colgroup><col width='12%'><col width='52%'><col width='10%'><col width='6%'><col width='8%'><col width='10%'><col width='2%'></colgroup>
  <tr class=bsm height=26>
   <th>Part #</th><th>Product Name</th><th>Price</th><th>Stock</th><th>Qty</th><th>Amount</th><th>X</th>
  </tr>
<%
	boolean ground = false;
	String rowspan = "";
	for(ItemsInBasket item : basket.getContents()) {
	    rowspan = (item.getWarrantyMask() & Item.EXTENDED_WARRANTY) != 0 ? "rowspan=2" : "";
%>
  <tr bgcolor=#ffff<%= (ground = !ground) ? "df" : "ff" %> align=center>
   <td <%= rowspan %> id=nowrap><%= item.getAccwareCode() %><div style='font-size:10px;color:blue;'><%= item.getCondition() %></div></td>
   <td align=left>
    <a href="<%= application.getInitParameter("http") + "Descr.jsp" %>?iid=<%= item.getItemID() %>" class=tlink><%= item.getItemName() %></a>
     <%= basketItems.getResellerPriceLine(item) %>
   </td>
   <td>
<%
    if(item.getResPrice1() != 0d) {
%>
      <div class=prices><%= priceForm.format(item.getPrice()) %></div>
<%
    }
%>
     <%= priceForm.format(item.getSellPrice()) %><br/>
   </td>
   <td <%= rowspan %>>
     <%= item.getStock() ? "Yes" : Integer.toString(item.getQuantity()) %>
     <input type=hidden name='qtyID' value='<%= item.getItemID() %>'/>
   </td>
   <td <%= rowspan %>><input type=text name='quantity' size=2 maxlength=3 value='<%= item.getTotalQuantity() %>' onChange="this.value=isInt(this.value);"/></td>
   <td><%= priceForm.format(item.getSellPrice() * item.getTotalQuantity()) %></td>
   <td <%= rowspan %>><input type=checkbox name=delete value='<%= item.getItemID() %>' onClick='document.basket.submit();'/></td>
  </tr>
<%
    // warranty &&  extended warranty any bit up ?
    if((item.getWarrantyMask() & Item.EXTENDED_WARRANTY) != 0) {
%>
  <tr bgcolor=#ffff<%= ground ? "df" : "ff" %> align=center>
   <td align=left>
<%
    // manufacture warranty any bit up ?
    if((item.getWarrantyMask() & item.MANUFACTURE_WARRANTY) != 0) {
        String warrantyDay = "";
        if((item.getWarrantyMask() & Item.MANUFACTURE_W30D) != 0)       warrantyDay = Item.W30DAYS;
        else if((item.getWarrantyMask() & Item.MANUFACTURE_W45D) != 0)  warrantyDay = Item.W45DAYS;
//      else if((item.getWarrantyMask() & Item.MANUFACTURE_W90D) != 0)  warrantyDay = Item.W90DAYS;
        else if((item.getWarrantyMask() & Item.MANUFACTURE_W180D) != 0) warrantyDay = Item.W180DAYS;
        else if((item.getWarrantyMask() & Item.MANUFACTURE_W1Y) != 0)   warrantyDay = Item.W1YEAR;
        else if((item.getWarrantyMask() & Item.MANUFACTURE_W3Y) != 0)   warrantyDay = Item.W3YEARS;
        else if((item.getWarrantyMask() & Item.MANUFACTURE_W5Y) != 0)   warrantyDay = Item.W5YEARS;
%>
    <div style='font-size:10px;color:black;'>Warranty : <%= warrantyDay %></div>
<%
                }
%>
   <a href='<%= application.getInitParameter("info") + "ExtWarranty.jsp" %>'><font color=#cc0000>Extended warranty : </font></a>
    <select name='warranty' size=1 style='width:150px;font-size:8pt;'>
     <option value='0|0.00'/>No Thanks
<%
                int wpos = 1;
                StringBuilder wbuff = new StringBuilder(200);
                if((item.getWarrantyMask() & Item.WARRANTY_30D) != 0)
                    wbuff.append(selectWarranty(wpos++, item.getWarrantyPrice1(), item.getWarrantyPrice2(), item.getWarrantyPrice3(), Item.W30DAYS));
                if((item.getWarrantyMask() & Item.WARRANTY_60D) != 0)
                    wbuff.append(selectWarranty(wpos++, item.getWarrantyPrice1(), item.getWarrantyPrice2(), item.getWarrantyPrice3(), Item.W60DAYS));
                if((item.getWarrantyMask() & Item.WARRANTY_90D) != 0)
                    wbuff.append(selectWarranty(wpos++, item.getWarrantyPrice1(), item.getWarrantyPrice2(), item.getWarrantyPrice3(), Item.W90DAYS));
                if((item.getWarrantyMask() & Item.WARRANTY_180D) != 0)
                    wbuff.append(selectWarranty(wpos++, item.getWarrantyPrice1(), item.getWarrantyPrice2(), item.getWarrantyPrice3(), Item.W180DAYS));
                if((item.getWarrantyMask() & Item.WARRANTY_1Y) != 0)
                    wbuff.append(selectWarranty(wpos++, item.getWarrantyPrice1(), item.getWarrantyPrice2(), item.getWarrantyPrice3(), Item.W1YEAR));
                if((item.getWarrantyMask() & Item.WARRANTY_3Y) != 0)
                    wbuff.append(selectWarranty(wpos++, item.getWarrantyPrice1(), item.getWarrantyPrice2(), item.getWarrantyPrice3(), Item.W3YEARS));
                if((item.getWarrantyMask() & Item.WARRANTY_5Y) != 0)
                    wbuff.append(selectWarranty(wpos++, item.getWarrantyPrice1(), item.getWarrantyPrice2(), item.getWarrantyPrice3(), Item.W5YEARS));
                if((item.getWarrantyMask() & Item.WARRANTY_LIFE) != 0)
                    wbuff.append(selectWarranty(wpos++, item.getWarrantyPrice1(), item.getWarrantyPrice2(), item.getWarrantyPrice3(), Item.WLIFETIME));

                String repl = String.format("|%.2f'", item.getWarrantyPrice());
                out.write(wbuff.toString().replace(repl, repl + " selected=selected"));
%>
    </select>
   </td>
   <td><%= item.getWarrantyPrice() == 0d ? "" : priceForm.format(item.getWarrantyPrice()) %></td>
   <td><%= item.getWarrantyPrice() == 0d ? "" : priceForm.format(item.getWarrantyTotal()) %></td>
  </tr>
<%
	    }
	    else {
                out.write("<input type=hidden name=warranty value=0 />");

	    }
        }
%>
  <tr height=28><td colspan=7 align=right valign=middle style='padding:0 35px;'>Total Amount : <%= priceForm.format(basket.getTempTotal()) %></td></tr>
 </table>
 <table width='80%' background='../images/tbg.png'>
  <tr>
   <td><a class=btlgr href='<%= session.getAttribute("backItemsPage") == null ? application.getInitParameter("http") : (String)session.getAttribute("backItemsPage") %>'>Continue Shopping</a></td>
   <td width='80%'>&nbsp;</td>
   <td><a class='btgr hbl2' href='javascript:void(0);' onClick="javascript:window.open('<%= application.getInitParameter("info") %>PopPolicies.jsp','','toolbar=no,scrollbars=yes,width=650,height=650,left=50,top=50');">Return<br/>Policy</a></td>
   <td><a class='btgn hbl2' href='javascript:void(0);' onClick="javascript:window.open('<%= application.getInitParameter("info") %>PopExtWarranty.jsp','','toolbar=no,scrollbars=yes,width=650,height=600,left=50,top=50');">Extended<br/>Warranty</a></td>
   <td><a class='btyw hbl1' href='#' onClick="document.basket.submit();">Update</a></td>
   <td><a class='btgr hbl1' href='<%= checkoutURL %>'>Check Out</a></td>
  </tr>
 </table>
<br/>

 <div class=htlt>Shipping Calculator</div>
 <table class=part width='80%' style='border:1px solid #d4d4d4;background-color:#ffffdf;'>
  <tr class=bsm height=26><td colspan=5>&nbsp;</td></tr>
  <tr>
   <td align=right>Country:&nbsp;&nbsp;</td>
   <td>
    <select class='cntry' onChange="document.basket.submit();" name="CountryCode">
    <%= advertArray.getCountryList(countryCode) %>
    </select>
   </td>
   <td  align=right>&nbsp;Enter your ZIP:</td>
   <td><input class=zip type=text value='<%= zipCode %>' maxLength=10 size=15 name="ZipCode"/></td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td colspan=4>&nbsp;</td>
   <td align=right><a class='btgr hbl1' href='#' onClick='document.basket.submit();'>Update</a></td>
  </tr>
  <tr><td colspan=5></td></tr>
<%
	// not show if zipCode=empty
	if(!"".equals(zipCode) || !"US".equals(countryCode)) {
%>
  <tr>
<%
	    java.util.List<ShipRate> shippRate = null;
	    if("US".equals(countryCode))
		shippRate = sb.getShippingChartUS(zipCode, basket.getTotalWeight());
	    else
		shippRate = sb.getShippingChart(countryCode, basket.getTotalWeight());

	    StringBuilder jsc = new StringBuilder(1000);
	    StringBuilder rdo = new StringBuilder(1000);
	    rdo.append("<td valign=middle align=right>Shipping method :&nbsp;&nbsp;</td>");
	    rdo.append("<td style='border:1px solid #d4d4d4;'>");
	    jsc.append("<script><!--\nfunction upship(ship){");

	    for(com.compuvest.web.ShipRate ship : shippRate) {

		rdo.append("<input type=radio name='ship_opt' value='").append(ship.getShipCode());
		rdo.append("' onClick='upship(this.value);'/> ");
		rdo.append(priceForm.format(ship.getShipPrice())).append(" - ").append(ship.getDescription()).append("<br/>");

		jsc.append("if(ship=='").append(ship.getShipCode()).append("'){");
		jsc.append("document.basket.spb.value='").append(priceForm.format(ship.getShipPrice())).append("';");
		jsc.append("document.basket.totb.value='").append(priceForm.format(ship.getShipPrice() + basket.getTempTotal())).append("';");
		jsc.append("}");
	    }
	    rdo.append("</td>");
	    jsc.append("}//--></script>\n");

	    if(shippRate.size() != 0) {
		out.write(rdo.toString());
		out.write(jsc.toString());
%>
   <td colspan=2 valign=bottom align=right style='border:1px solid #d4d4d4;'>
    <span class=ship2>Shipping cost:<input class=zip name='spb' value='' size=15 readonly></span><br/>
    <span class=ship2>Grand total:<input class=zip name='totb' value='' size=15 readonly/></span>
   </td>
   <td></td>
  </tr>
  <tr class=bsm height=26 align=right><td colspan=5><a class='btgr hbl1' href='<%= checkoutURL %>'>Check Out</a></td></tr>
<%
            }
        }
%>
 </table>
 </form>

<%
    } //(basket.size != 0)
    else {
%>
<br/>
<span class=htlt>There are currently no items in your shopping cart.</span>
<br/>
<a class=btlgr href='<%= session.getAttribute("backItemsPage") == null ? application.getInitParameter("http") : (String)session.getAttribute("backItemsPage") %>'>Continue Shopping</a>
<%
    }
%>
</td>

 <td style="width:160px;">&nbsp;</td>
</tr>

<tr height=35>
 <td colspan=3>

<bottom:show />

 </td>
</tr>
</table>

</body>
<%@ include file="./incl/tracking.incl" %>
</html>
